<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/19 0019
 * Time: 08:58
 */

namespace app\core\controller;
use think\Request;

class Error
{
    public static $code = [
        '100000'=>'帐号不存在',
        '101000'=>'帐号已经存在',
        '102000'=>'帐号已经被禁用',
        '103000'=>'确认密码不能为空',
        '104000'=>'两次密码不相同',
        '800000'=>'验证码已经过期',
        '801000'=>'验证码错误',
        '999988'=>'您的本次消耗不足',
        '999989'=>'暂无数据',
        '999990'=>'',
        '999991'=>'短信模版不存在',
        '999992'=>'短信发送失败',
        '999993'=>'必传参数数据不可为空',
        '999994'=>'必传参数数据不可为零或空',
        '999995'=>'必传参数不存在',
        '999996'=>'COMMAND为必传参数',
        '999997'=>'方法不存在',
        '999998'=>'系统错误',
        '999999'=>'错误码不存在',

        '200001'=>'没有该任务',

        '300001'=>'不能赠送给自己盒子',
        '300002'=>'赠送盒子数大于拥有的',
        '300003'=>'您的盒子数不足',
    ];

    //变量
    public $data,$in;

    /**
     * @错误返回
     * @param string $code
     * @return array
     */
    public function error($code,$txt='',$type=0){
        if(isset(self::$code[$code])){
            $out['code'] = $code;
            if($type==0){
                $out['msg'] = self::$code[$code].$txt;
            }else{
                $out['msg'] = $txt;
            }
        }else{
            $out['code'] = '999999';
            $out['msg'] = self::$code[$out['code']].',未定义'.$code.'的错误说明';
        }
        $out['data'] = '';
        return $out;
    }

    /**
     * @成功返回
     * @param string $code
     * @return array
     */
    public function success($data,$txt=''){
        $out['code'] = '000000';
        $out['msg'] = empty($txt)?'操作成功':$txt;
        $out['data'] = $data;
        return $out;
    }

    /**
     * 统一输入
     * @param $method
     * @return mixed|string
     */
    public function auto($method='POST'){
//        origin();//跨域
        //接收数据
        $req = Request::instance();
        //接收头部信息
        $head = $req->header();
//        return $head;
        //接收BODY数据
        $this->in = $req->param();
        //提交方式判断
        if($req->method() != strtoupper($method)){
            return false;
        }
        return true;
    }

    /**
     * 统一输出
     * @return mixed|string
     */
    public function out($code='000000',$txt='',$type=0){
        if($code=='000000'){
            $re = $this->success($this->data,$txt);
        }else{
            $re = $this->error($code,$txt,$type);
        }
        return json_encode($re);
    }

    /**
     * @必传值判断 如果$key或者$this->arr有一个为空，则不在判断
     * @param array $wVal 二维数组(0:key,1:条件)
     * @param array $data 传过来的总数据
     * @return array
     */
    public function will($wVal,$data){
        if(!$wVal){
            return $this->success($data);
        }
        if(!$data){
            return $this->success($data);
        }
        //找出不存的KEY
        $diff = array_diff_key($wVal,$data);
        $key = array_keys($diff);
        if($key){
            $keytxt = implode(',',$key);//数组转字符串
            return $this->error('999995','|'.$keytxt);
        }
        //找出集合
        $intersect = array_intersect_key($wVal,$data);
        foreach ($intersect as $k => $v){
            if($v['isZero']){
                if(empty((float)$data[$k])){
                    return $this->error('999994','|'.$k);
                }
            }else{
                if($data[$k]==""){
                    return $this->error('999993','|'.$k);
                }
            }
        }
        return $this->success($data);
    }
}